import request from "@/utils/request";
import axios from "axios";
import store from "@/store";
import { getToken } from "@/utils/auth";
import host from "@/utils/host";

function dateFormat(date) {
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, "0"); // 月份从 0 开始，需要加 1
  let day = date.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day}`;
}

function getParamsForPrint(
  borrowTime,
  returnTime,
  borrowById,
  barcode,
  airplaneId
) {
  borrowById = borrowById || "-1";
  barcode = barcode || "-1";
  let returnTime1 = "_",
    returnTime2 = "_",
    borrowTime1 = "_",
    borrowTime2 = "_";
  if (borrowTime.length == 2) {
    //如果长度为2表示有数据
    borrowTime1 = dateFormat(borrowTime[0]);
    borrowTime2 = dateFormat(borrowTime[1]);
  }
  if (returnTime.length == 2) {
    //如果长度为2表示有数据
    returnTime1 = dateFormat(returnTime[0]);
    returnTime2 = dateFormat(returnTime[1]);
  }
  if (borrowTime.length == undefined) {
    borrowTime = dateFormat(borrowTime);
  }
  if (returnTime.length == undefined) {
    returnTime = dateFormat(returnTime);
  }
  const params = {
    borrowTime1,
    borrowTime2,
    returnTime1,
    returnTime2,
    borrowById,
    barcode,
    airplaneId,
  };

  return params;
}

function getParamsForShow(
  borrowTime,
  returnTime,
  borrowById,
  barcode,
  airplaneId,
  currentPage,
  pageSize
) {
  borrowById = borrowById || "-1";
  barcode = barcode || "-1";
  let returnTime1 = "_",
    returnTime2 = "_",
    borrowTime1 = "_",
    borrowTime2 = "_";
  if (borrowTime && borrowTime.length == 2) {
    //如果长度为2表示有数据
    borrowTime1 = dateFormat(borrowTime[0]);
    borrowTime2 = dateFormat(borrowTime[1]);
  }
  if (returnTime && returnTime.length == 2) {
    //如果长度为2表示有数据
    returnTime1 = dateFormat(returnTime[0]);
    returnTime2 = dateFormat(returnTime[1]);
  }
  const params = {
    borrowTime1,
    borrowTime2,
    returnTime1,
    returnTime2,
    borrowById,
    barcode,
    airplaneId,
    currentPage,
    pageSize,
  };

  return params;
}

function getParamsForAll(
  borrowTime,
  returnTime,
  borrowById,
  barcode,
  airplaneId
) {
  borrowById = borrowById || "-1";
  barcode = barcode || "-1";
  let returnTime1 = "_",
    returnTime2 = "_",
    borrowTime1 = "_",
    borrowTime2 = "_";
  if (borrowTime && borrowTime.length == 2) {
    //如果长度为2表示有数据
    borrowTime1 = dateFormat(borrowTime[0]);
    borrowTime2 = dateFormat(borrowTime[1]);
  }
  if (returnTime && returnTime.length == 2) {
    //如果长度为2表示有数据
    returnTime1 = dateFormat(returnTime[0]);
    returnTime2 = dateFormat(returnTime[1]);
  }
  const params = {
    borrowTime1,
    borrowTime2,
    returnTime1,
    returnTime2,
    borrowById,
    barcode,
    airplaneId,
  };
  return params;
}

export default {
  printPage: async function (
    borrowTime,
    returnTime,
    borrowById,
    barcode,
    airplaneId
  ) {
    const params = getParamsForPrint(
      borrowTime,
      returnTime,
      borrowById,
      barcode,
      airplaneId
    );
    const res = await axios.get(`http://${host}:8081/T01/print/printHistory`, {
      params,
    });
    if (!res.data.code) {
      const win = window.open("TMS");
      win.document.write(res.data);
      win.print();
      win.close();
    } else {
      alert(res.data.message);
    }
  },
  list(
    borrowTime,
    returnTime,
    borrowById,
    barcode,
    airplaneId,
    currentPage,
    pageSize
  ) {
    const params = getParamsForShow(
      borrowTime,
      returnTime,
      borrowById,
      barcode,
      airplaneId,
      currentPage,
      pageSize
    );
    return request({
      url: `/history/list`,
      method: "get",
      params,
    });
  },
  total(borrowTime, returnTime, borrowById, barcode, airplaneId) {
    const params = getParamsForAll(
      borrowTime,
      returnTime,
      borrowById,
      barcode,
      airplaneId
    );
    return request({
      url: `/history/total`,
      method: "get",
      params,
    });
  },
  download(borrowTime, returnTime, borrowById, barcode, airplaneId) {
    let jwt = "your_jwt_string";
    if (store.getters.token) {
      jwt = getToken();
    }
    const url = `http://${host}:8081/T01/history/download`;
    const params = getParamsForAll(
      borrowTime,
      returnTime,
      borrowById,
      barcode,
      airplaneId
    );
    axios
      .get(url, {
        headers: {
          Authorization: `${jwt}`, // 在请求头中添加 JWT 字符串
          "Content-Type": "application/json", // 在请求头中添加 Content-Type
        },
        params: params,
        responseType: "blob", // 指定响应类型为二进制数据
      })
      .then((response) => {
        const blob = new Blob([response.data], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // 设置为正确的 MIME 类型
        });
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", `历史记录.xlsx`);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      })
      .catch((error) => {
        alert("下载文件出错：" + error.message || error.data);
      });
  },
};
